package com.kx.center.service.impl;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.kx.center.common.Constant;
import com.kx.center.entity.MsgSend;
import com.kx.center.entity.Suggestion;
import com.kx.center.mappers.MsgNoticeMapper;
import com.kx.center.mappers.SuggestionMapper;
import com.kx.center.service.SendEmailService;
import com.kx.center.service.SuggestionService;
import com.kx.center.util.DateUtil;
import com.kx.center.util.FileUploadUtils;
import com.kx.center.util.JpushUtil;

@Service
public class SuggestionServiceImpl implements SuggestionService{
	
	@Autowired
	SuggestionMapper suggestionMapper;
	
	@Autowired
	SendEmailService emailService;
	
	@Autowired
	MsgNoticeMapper noticeMapper;

	/*
	 * 添加意见反馈
	 */
	@Override
	public void addSuggestion(HttpServletRequest request, Suggestion suggestion) {
		if(suggestion != null && suggestion.getAccoutId() > 0){
			//1.保存图片，如果有图，则返回图片保存路径及图片名 [数组形式]
			List<String> imagesPath = FileUploadUtils.tranferFile(request, Constant.SUGGESTION_PATH +"/" + suggestion.getAccoutId());
			StringBuilder sb = new StringBuilder();
			//2.取出数组中的图片名，以字符分割  ，同内容一块保存到数据库中
			if(imagesPath != null && imagesPath.size() > 0){
				for(int i=0; i<imagesPath.size(); i++){
					if(i < imagesPath.size() -1){
						sb.append(imagesPath.get(i) + ",");
					}else{
						sb.append(imagesPath.get(i));
					}
				}
			}
			if(sb.length() > 0){
				suggestion.setImages(sb.toString());
			}
			suggestionMapper.addSuggestion(suggestion);
			//给管理员发送邮件
			//根据id查询要接收邮件的email地址
			List<String> emailList = suggestionMapper.findAcceptEmail(suggestion.getCompanyId());
			
			//发送邮件
			Map<String,Object> model = new HashMap<>();
			
			Calendar calendar = Calendar.getInstance();
			int year = calendar.get(Calendar.YEAR);
			int month = calendar.get(Calendar.MONTH) + 1;
			int day = calendar.get(Calendar.DAY_OF_MONTH);
			String time = DateUtil.formatTime(new Date());
			
			model.put("userId", suggestion.getUserId());
			model.put("year", year);
			model.put("month", month);
			model.put("day", day);
			model.put("time", time);
			model.put("url", Constant.WEB_MANAGER_URL);
			
			if(emailList != null && emailList.size() > 0){
				emailService.sendEmailFromTemplate(Constant.VM_SUGGESTION, emailList, model);
			}
			
		}
		
	}

	@Override
	public List<Suggestion> findListByPage(int companyId ,int pageNow ,int pageSize) {
		
		
		List<Suggestion> suggestionList = suggestionMapper.findList(companyId ,(pageNow -1)*pageSize ,pageSize);
		
		for(int i=0; i<suggestionList.size(); i++){
			//取出accountId 查找门店名称 
			Suggestion suggestion = suggestionList.get(i);
			int accountId = suggestion.getAccoutId();
			//查找门店名称
			Map<String,Object> accountInfo = suggestionMapper.findAccountInfo(accountId);
			
			suggestion.setUserId((String)accountInfo.get("user_id"));
			suggestion.setUserName((String)accountInfo.get("user_name"));
			suggestion.setStoreName((String)accountInfo.get("store_name"));
			
			//查找操作者姓名
			int operatorId = suggestion.getOperatorId();
			String operatorName = suggestionMapper.findOperatorName(operatorId);
			suggestion.setOperatorName(operatorName);
			
		}
		
		return suggestionList;
	}

	@Override
	public Map<String, Object> findSuggestionDetail(int suggestionId) {
		
		
		return suggestionMapper.findSuggestionDetail(suggestionId);
	}

	@Override
	public void replySuggestion(int id, String replyContent, int operatorId ,int accountId) {
		suggestionMapper.replySuggestion(id,replyContent,operatorId);
		//根据accountId查询用户userid 
		String userId = suggestionMapper.findUserIdById(accountId);
		//回复成功  推送消息
		String alias = accountId + userId;
		MsgSend msgSend = noticeMapper.findMsgSendById(id);
		JpushUtil.noticeMsgPush(alias, JpushUtil.SUGGESTION_REPLY, msgSend);
	}

	@Override
	public void cancelSuggest(int id) {
		
		suggestionMapper.cancelSuggest(id);
	}

	@Override
	public List<Suggestion> findFrontList(int accountId) {
		
		return suggestionMapper.findFrontList(accountId);
	}

	@Override
	public Map<String, Object> findFrontDetail(int id) {
		
		return suggestionMapper.findSuggestionDetail(id);
	}

	@Override
	public int findPageCount(int companyId) {
		
		return suggestionMapper.findPageCount(companyId);
	}

}









